﻿using Max.PH4_2102A.Shopping.Domain;
using Max.PH4_2102A.Shopping.DTO;
using Max.PH4_2102A.Shopping.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH4_2102A.Shopping.API.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("user")]
    [ApiController]
    public class UserController : ControllerBase
    {
        //注入服务接口
        IUserService _service;

        public UserController(IUserService service)
        {
            _service = service;
        }


        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAll(int pageIndex=1,int pageSize =10)
        {
            ResponseResultDto result = new();

            var data = _service.GetAll("", pageIndex, pageSize, out int total);

            result.Data = new { items = data, total }; //new { data, total };

            return Ok(result);

            //var data = _service.GetAll("", pageIndex, pageSize, out int total);
            //return Ok(new { data,total});
        }

        /// <summary>
        /// 根据ID获取详情信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("first")]
        public IActionResult GetFirstById(string id)
        {
            ResponseResultDto result = new();

            var s = _service.GetAll(); 


            var fisrt = _service.GetAll().Where(x => id.Equals(x.Id.ToString())).FirstOrDefault();
            result.Data = fisrt;

            return Ok(result);
        }


        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Insert(UserInsertInputDto model)
        {
            ResponseResultDto result = new ResponseResultDto();
            _service.Insert(model);
            //return Ok();

            /*
             * 统一返回值：code、message、data | timestramp
             * code：状态码。就是给前台开发人员用来判断是否操作成功的
             * message：提示信息。就前台的使用用户提示的
             * data：数据。是用来存储后台给前台的数据
             * timestramp：时间戳。
             * **/

            //var data = new { code = 200,mssage = "操作成功" };
            return Ok(result);
        }


        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("login")]
        public IActionResult Login(UserLoginInputDto model)
        {
            ResponseResultDto result = new ResponseResultDto();
            var data = _service.Login(model.UserName, model.Password);

            if (!data.Item1)
            {
                result.Code = ResponseStateCode.Failed;
                result.Message = data.Item2;
            }
            else
            {
                result.Data = data.Item3;
            }
            return Ok(result);

            //return Ok(new { state = result.Item1,message = result.Item2,data = result.Item3});
        }


        /// <summary>
        /// 锁定用户
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet("lock")]
        public IActionResult LockUser(string phone)
        {
            ResponseResultDto result = new();

            int d = _service.LockUser(phone);
            if(d <= 0)
            {
                result.Code = ResponseStateCode.Failed;
                result.Message = "操作失败";
            }

            return Ok(result);
        }


        /// <summary>
        /// 验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet("code")]
        public IActionResult Code()
        {
            ResponseResultDto result = new();
            //验证码三种：1、纯数字   2、数字和字母  3、图片形式
            //只演示前面两种

            //纯数字
            //var code = new Random().Next(1000, 9999);

            //数字和字母结合
            string[] s = new string[4];
            var str = "abcdefghijkmnpqrstuvwxyzABCDEFJHJKMNPQRSTUVWXYZ23456789";
            var cs = str.ToArray(); //将字符串变成数组
            for (int i = 0; i < 4; i++)
            {
                //随机因子是str有多少个字符，然后将得到的随机数为字符数组下标，将对应的值存入一个数组中
                s[i] = cs[new Random().Next(0, cs.Length)].ToString();
            }

            //将数组拼接成字符串
            var code = string.Join("", s);


            result.Data = code;

            return Ok(result);
        }



    }
}
